﻿<script type="text/javascript">
    var oldSelectDepartmentId;
    $(function () {
        getButton("department");
    });

    function ui_department_init_list(toolbar) {
        $("#ui_department_tg").treegrid({       //初始化treegrid
            url: "ashx/rm_department.ashx?action=getall",
            idField: 'id',
            treeField: 'text',
            rownumbers: true,
            columns: [[
                        { field: 'text', title: '部门名称', width: 250 },
                        { field: 'code', title: '部门编码', width: 100 },
                        { field: 'headName', title: '部门领导', width: 100 },
                        { field: 'sort', title: '排序', width: 50 },
                        { field: 'addDate', title: '创建时间', width: 150 }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar            
        });
    }
    //添加部门
    function ui_department_add() {
        $("<div/>").dialog({
            id: "ui_department_add_dialog",
            href: "html/ui_department_edit.html",
            title: "添加部门",
            iconCls:"icon-add",
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_department_add_btn",
                text: '添 加',
                iconCls: "icon-add",
                handler: function () {
                    $("#ui_department_editform").form("submit", {
                        url: "ashx/rm_department.ashx",
                        onSubmit: function (param) {
                            $('#ui_department_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_department_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_department_add_dialog").dialog('destroy'); //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_department_tg").treegrid("reload");
                            } else {
                                $('#ui_department_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_department_name_edit").focus();
            },
            onClose: function () {
                $("#ui_department_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //修改部门
    function ui_department_edit() {
        var row = $("#ui_department_tg").treegrid("getSelected");
        if (!row) {
            $.show_warning("提示", "请先选择要修改的部门");
            return;
        }
        $("<div/>").dialog({
            id: "ui_department_edit_dialog",
            href: "html/ui_department_edit.html",
            title: "修改部门",
            iconCls: "icon-edit",
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_department_edit_btn",
                text: '修 改',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_department_editform").form("submit", {
                        url: "ashx/rm_department.ashx",
                        onSubmit: function (param) {
                            $('#ui_department_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_department_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_department_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_department_tg").treegrid("reload");
                            } else {
                                $('#ui_department_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_department_id_edit").val(row.id);
                $("#ui_department_name_edit").textbox("setValue",row.text);
                $("#ui_department_code_edit").textbox("setValue", row.code);
                $('#ui_department_sort_edit').numberspinner('setValue', row.sort);
                if(row.parentId >0)
                    $('#ui_department_parentid_edit').combotree('setValue', row.parentId);
                if (row.headUid > 0)
                    $('#ui_department_head_edit').combotree('setValue', row.headUid);
            },
            onClose: function () {
                $("#ui_department_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //删除部门
    function ui_department_delete() {
        var row = $("#ui_department_tg").datagrid("getSelected");
        if (!row) {
            $.show_warning("提示", "请先勾选要删除的部门");
            return;
        }
        
        $.messager.confirm('提示', '确定删除：' + row.text + '？如果有子部门也会同时删除！', function (r) {
            if (r) {                
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.ids = row.id;

                $.ajax({
                    url: "ashx/rm_department.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_department_tg").treegrid("reload");
                            //如果“用户管理”标签页处于打开状态需要刷新，否则被删除的部门还显示，再点编辑用户就出错
                            if ($('#tabs').tabs('exists', '用户管理')) {
                                $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                            }
                            $("#ui_department_layout").layout("collapse", "east");
                            $("#ui_department_user_dg").datagrid('loadData', { total: 0, rows: [] });   //清空
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
    
    function ui_department_expandall() {
        $("#ui_department_tg").treegrid("expandAll");
    }
    function ui_department_collapseall() {
        $("#ui_department_tg").treegrid("collapseAll");
    }
</script>
<div id="ui_department_layout" class="easyui-layout" data-options="fit:true,border:false">
    <!--div data-options="region:'east',split:true,border:true,collapsed:true" title="部门成员"
        style="width: 480px;">
        <table id="ui_department_user_dg" data-options="fit:true,border:false">
        </table>
    </div-->
    <div data-options="region:'center',border:false">
        <table id="ui_department_tg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
